/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using Whitemice.ZOGI;
using Whitemice.ZOGI.GtkWidgets;
using Whitemice.ZOGI.GtkForms;
using Gtk;

namespace Whitemice.ZOGI.GtkFolios
{
  public partial class EnterpriseSearchDialog : Gtk.Dialog, 
                                                IZOGIWidget
  {
		protected EnterpriseNodeView	  enterpriseList;
		protected int									  enterpriseId;
		public delegate void 				    SetEnterpriseIdHandler(int enterpriseId);
		public SetEnterpriseIdHandler   SetEnterpriseId;
		protected MultiResultCallback   callback;
    protected EnterpriseQBEDialog                    qbeDialog;
		
		/* @brief Constructor */
		public EnterpriseSearchDialog(
		  Window _parent, 
		  int _enterpriseId, 
		  string _title)
			: base(_title, _parent, Gtk.DialogFlags.DestroyWithParent)
		{
			ScrolledWindow	sw;
			
			this.enterpriseId = _enterpriseId;
			this.Build();
			Title = _title;
			/* Setup enterprise list widget */
			sw = new ScrolledWindow();
			enterpriseList = new EnterpriseNodeView(null, true);
			sw.Child = enterpriseList;
			enterpriseListSpace.Child = sw;
			enterpriseList.NodeSelection.Changed += 
				new System.EventHandler(OnSelectionChanged);
		  /* Show all the widgets; is this required? */
			this.ShowAll();
			callback = new MultiResultCallback(this.ReceiveDataCallback);
		}
		
    /* @brief Determine if popup menu in contact search results is enabled */
		public bool PopupEnabled
		{
			get { return enterpriseList.PopupEnabled; }
			set { enterpriseList.PopupEnabled = value; }
		} /* End PopupEnabled */
		
		/* @brief Handle selection change event in contact search results */
		protected void OnSelectionChanged (object sender, System.EventArgs args)
		{
			Gtk.NodeSelection	  selection;
			EnterpriseListNode  node;
			
			selection = (Gtk.NodeSelection)sender;
			if (selection.SelectedNode == null)
			{
				enterpriseId = 0;
			} else
				{
      		node = (EnterpriseListNode)selection.SelectedNode;
      		enterpriseId = node.Enterprise.ObjectId;
      	}
      this.ZLOG("Selected EnterpriseId#{0}", enterpriseId);
		}	// End OnSelectionChanged
		
		public void ReceiveRPCCall(RPCEventArgs args)
		{
      if (args.HasResults)
		    enterpriseList.Fill(args.Payload, null);
		} // End SearchResultCallback

    /* @brief Prepare and issue search when Find button is clicked */
		protected void OnFindClicked(object sender, System.EventArgs e)
		{
      EnterpriseSearchCommand command;

      command = new EnterpriseSearchCommand(0, "EnterpriseSearch");
      command.Callback = callback;
      command.AddORCriteria("name", 
                            string.Format("*{0}*", searchCriteria.Text),
                            true,
                            false);
			this.Modal = true;
      Server.Connection.RunCommand(command);
		} // End OnFindClicked

    /* @brief Clear the search results */
		protected void OnClearClicked(object sender, System.EventArgs e)
		{
			enterpriseId = 0;
			enterpriseList.NodeStore.Clear();
		} // end OnClearClicked

    /* @brief Send the selected contact and hide window */
		protected void OnOkClicked(object sender, System.EventArgs e)
		{
      this.ZLOG("Ok EnterpriseId#{0}", enterpriseId);
			this.SetEnterpriseId(enterpriseId);
			this.Hide();
		} // end OnOkClicked
		
		/* @brief Hide window without sending selected contact */
		protected void OnCancelClicked(object sender, System.EventArgs e)
		{
			this.Hide();
		}

     protected virtual void OnQBEClicked (object sender, System.EventArgs e)
    {
      if (qbeDialog == null)
      {
        qbeDialog = new EnterpriseQBEDialog(
          null, 
          Gtk.DialogFlags.DestroyWithParent);
        qbeDialog.OnQBEDialogOk += new System.EventHandler(OnQBEOk);
        qbeDialog.OnQBEDialogCancel += new System.EventHandler(OnQBECancel);
        qbeDialog.Destroyed += new System.EventHandler(OnQBEDestroyed);
      } else
        {
          qbeDialog.Show();
          qbeDialog.Present();
        }
     }   // end OnCancelClicked

    protected virtual void OnQBEOk(object sender, EventArgs args)
    {
      this.ZLOG("OnQBEOk");
      qbeDialog.Hide();
      var command = qbeDialog.GetQuery(65535, "EnterpriseSearch");
      command.Callback = callback;
      Server.Connection.RunCommand(command);
    } // end OnQBEOk
    
    protected virtual void OnQBECancel(object sender, EventArgs args)
    {
      qbeDialog.Hide();
    } // end OnQBECancel
    
    protected virtual void OnQBEDestroyed(object sender, EventArgs args)
    {
      qbeDialog = null;
    } // end OnQBEDestroyed
  }
}
